package com.ww.java8.fp.stream.inaction.ch04;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class StreamBasic {

	public static void main(String[] args) {
		getLowCaloricDishesNamesInJava7(Dish.menu).forEach(System.out::println);
		System.out.println("--------");
		getLowCaloricDishesNamesInJava8(Dish.menu).forEach(System.out::println);
	}
	
	public static List<String> getLowCaloricDishesNamesInJava7(List<Dish> dishes) {
		List<Dish> lowlowCaloricDishes = new ArrayList<>();
		// 过滤
		for (Dish d : dishes) {
			if (d.getCalories() < 400) {
				lowlowCaloricDishes.add(d);
			}
		}
		
		List<String> lowCaloricDishesName = new ArrayList<>();
		// 排序
		Collections.sort(lowlowCaloricDishes, new Comparator<Dish>() {
			@Override
			public int compare(Dish o1, Dish o2) {
				return Integer.compare(o1.getCalories(), o2.getCalories());
			}
		});
		
		for (Dish d : lowlowCaloricDishes) {
			lowCaloricDishesName.add(d.getName());
		}
		
		return lowCaloricDishesName;
	}
	
	/**
	 * 明显比Java8之前版本简洁很多
	 * @param dishes
	 * @return
	 */
	public static List<String> getLowCaloricDishesNamesInJava8(List<Dish> dishes) {
		return dishes.stream()
				.filter(d -> d.getCalories() < 400)
				.sorted(Comparator.comparing(Dish::getCalories))
				.map(Dish::getName)
				.collect(Collectors.toList());
	}
}
